#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;


namespace NativeTest_Question15
{	
	char* ReplaceSpace(char* str)
	{
		int spaceCount = 0;
		int length = 0;
		char* end = str;
		while (*end)
		{
			if(*end == ' ')
			{
				++spaceCount;
			}
			++end;
		}
		

		if(spaceCount != 0)
		{
			int newstrlen = strlen(str) + 2 * spaceCount + 1;
			char* newstr = new char[newstrlen];
			newstr[newstrlen -1] = '\0';
			char* result = newstr;
			while(*str)
			{
				if(*str == ' ')
				{
					*newstr++ = '%';
					*newstr++ = '2';
					*newstr++ = '0';
				}
				else
				{
					*newstr++ = *str;
				}

				++str;
			}
			return result;
		}
		else
		{
			return str;
		}
	}
}

namespace NativeTest
{
	using namespace NativeTest_Question15;
	TEST_CLASS(Question15)
	{
	public:
		
		TEST_METHOD(Question15_TestMethod1)
		{
			// TODO: Your test code here
			char str[] = "ReplaceSpace";
			ReplaceSpace(str);
			Assert::AreEqual("ReplaceSpace",str);
		}

		TEST_METHOD(Question15_TestMethod2)
		{
			char str[] = "Replace Space";
			char* newstr = ReplaceSpace(str);
			Assert::AreEqual("Replace%20Space",newstr);
		}
	};
}